package com.platform.api.common.component;

import com.platform.api.bzproject.model.BzConfig;
import com.platform.api.bzproject.service.BzConfigService;
import com.platform.common.core.Constant;
import com.platform.common.util.ImageUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Objects;

/**
 * @author guoyunfei on 2024/8/3 17:39
 * @className ImageCompressComponent
 * @description TODO
 * @Version 1.0
 */

@Component
public class ImageCompressComponent {

    @Autowired
    private BzConfigService bzConfigService;

    /**
     * 图片压缩
     * @param file  MultipartFile
     * @return
     * @throws IOException
     */
    public MultipartFile imageCompres (MultipartFile file) throws IOException {

        BzConfig bzConfig = bzConfigService.getByType(Constant.COMPRESSION_QUALITY);

        float num = 0.8f;

        if (Objects.nonNull(bzConfig)) {
            String quality = bzConfig.getValue();
            num = Float.parseFloat(quality);
        }

        return ImageUtils.compressImage(file, num);
    }

}
